package easier.camera;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.UseCase;
import androidx.camera.view.PreviewView;
import easier.camera.x.CameraXManager;

public class MainActivity2 extends AppCompatActivity {

    private PreviewView mPreviewView;
    CameraXManager mXManager;

    private int mFront = 0;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity2.class );
        starter.putExtra( "front", 0 );
        context.startActivity( starter );
    }

    public static void startFront( Context context ) {

        Intent starter = new Intent( context, MainActivity2.class );
        starter.putExtra( "front", 1 );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );

        Intent intent = getIntent();
        if ( intent != null ) {
            mFront = intent.getIntExtra( "front", 0 );
        }
        setContentView( R.layout.activity_main2 );
        initView();
        initViewAction();
    }

    private void initView() {

        mPreviewView = (PreviewView) findViewById( R.id.previewView );
    }

    private void initViewAction() {

        mXManager = new CameraXManager( this, this ) {
            @Override
            protected UseCase[] buildUseCases() {

                return new UseCase[]{
                    buildPreviewCase( mPreviewView )
                };
            }

            @NonNull
            @Override
            protected CameraSelector provideCameraSelector() {

                return mFront == 0 ? CameraSelector.DEFAULT_BACK_CAMERA : CameraSelector.DEFAULT_FRONT_CAMERA;
            }
        };
    }
}